<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="产废单位名称:">
          <el-input v-model="formInline.wpcName" clearable />
        </el-form-item>
        <el-form-item label="废物名称:">
          <el-input v-model="formInline.name" clearable />
        </el-form-item>
        <el-form-item label="废物代码:">
          <el-input v-model="formInline.codeContent" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">
      <el-form :inline="true" class="form-inline">
        <el-button type="primary" icon="el-icon-plus" @click="add">
          添加
        </el-button>

      </el-form>

      <div class="result">
        <el-table ref="multipleTable" v-loading="loading" border :data="tableData" tooltip-effect="dark"
                  style="width: 100%" @selection-change="handleSelectionChange" @select="selectInfo"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="name" label="废物名称" />
          <el-table-column prop="categoryContent" label="废物类别" />
          <el-table-column prop="codeId" label="废物代码" />
          <el-table-column prop="attributeContent" label="危险特性" />
          <el-table-column prop="physicalProperties" label="废物形态">
            <template slot-scope="scope">
              <el-tag size="medium" style="width: 100%; text-align: center;" effect="dark">
                {{ scope.row.physicalProperties |parsePhysicalProperties }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="hazardousSubstancesName" label="危害成分" />
          <el-table-column prop="containerCategory" label="容器类型">
            <template slot-scope="scope">
              <el-tag size="medium" style="width: 100%; text-align: center;" effect="dark">
                {{ scope.row.containerCategory | parseContainerCategory }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="inAmount" label="废物重量(吨)" />
        </el-table>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next,sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
        <!-- <div class="total-weight">当前页统计 总重量：{{ weightTotal }} 吨</div> -->
      </div>
    </div>
  </div>
</template>

<script>
// Table组件相关逻辑，需要引用
import {
  pageMixin
} from "../../../mixins/index";
import {
  PhysicalProperties,
  ContainerType
} from "../../../utils/resource.js";

import {
  getWastObjListByCode,
} from '../../../api/warehouse.js';
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },

    parsePhysicalProperties(value) {
      if (value != null && value != undefined && value != "") {
        var res = PhysicalProperties.find((p) => p.value === `${value}`);
        if(res != null) {
          return res.label;
        } else {
          return '';
        }
      }
    },
    parseContainerCategory(value) {
      if (value != null && value != undefined && value != "") {
        var res = ContainerType.find((p) => p.value === `${value}`);
        if(res != null) {
          return res.label;
        } else {
          return '';
        }
      }
    },

  },
  mixins: [pageMixin],
  data() {
    return {
      formInline: {
        name: "",
        codeContent: "",
        wpcName: "",
      },
      tableData: [],

      weightTotal: 0,

      tempFormInfo: {},

      // 当前选中的数据
      currentSelectedList: [],
    };
  },
  async created() {
    await this.init();
  },
  methods: {
    async init() {
      this.tempFormInfo = JSON.parse(localStorage.getItem('tempFormInfo'));
      await this.loadingInfo();
    },
    async handleSizeChange(val) {
      await this.loadingInfo();
    },
    async handleCurrentChange(val) {
      await this.loadingInfo();
    },
    async  loadingInfo() {
      this.tableData = [];
      this.loading = true;

      // var transferPlanId = "";
      // if (this.tempFormInfo) {
      //   transferPlanId = this.tempFormInfo.transportPlanId
      // }

      const {
        data,
        count
      } = await getWastObjListByCode({
        limit: this.pageSize,
        page: this.currentPage,
        searchParam: JSON.stringify({
          Name: this.formInline.name,
          CodeContent: this.formInline.codeContent,
          WpcName: this.formInline.wpcName,
          Status: "10,30"
        })
      });
      data.forEach(item => {
        item.wastWeight = 0;
      })
      this.tableData = data;
      console.log(data);
      console.log(232);
      this.total = count;
      this.loading = false;

      if (this.tempFormInfo) {
        this.currentSelectedList = [];

        var Data = this.tempFormInfo.tableData;

        this.tableData.forEach(item => {
          var mm = Data.findIndex(target => {
            return target.id === item.id
          })
          if (mm > -1) {
            this.currentSelectedList.push(item)
          }
        })

        // 等待表格内容加载完成
        this.$nextTick(() => {
          this.toggleSelection(this.currentSelectedList)
        })
      }
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },

    selectInfo(val) {
      if (val.length > this.currentSelectedList.length) {
        val.forEach(item => {
          var mm = this.currentSelectedList.findIndex(target => {
            return target.id === item.id
          })
          if (mm === -1) {
            this.tempFormInfo.tableData.push(item);
          }
        })
      } else {
        this.currentSelectedList.forEach(item => {
          var mm = val.findIndex(target => {
            return target.id === item.id
          })
          if (mm === -1) {
            var num = this.tempFormInfo.tableData.findIndex(target => {
              return target.id === item.id
            })
            if (num > -1) {
              this.tempFormInfo.tableData.splice(num, 1);
            }
          }
        })
      }
      this.currentSelectedList = val;

      localStorage.setItem('tempFormInfo', JSON.stringify(this.tempFormInfo));
      console.log(this.tempFormInfo.tableData);
    },
    async search() {
      await this.init();
    },
    add() {
      const arr = this.$refs.multipleTable.selection;

      let selectedList = [];
      if (this.tempFormInfo) {
        var tableData = this.tempFormInfo.tableData;

        arr.forEach(item => {
          var mm = tableData.findIndex(target => {
            return target.id === item.id
          })
          if (mm === -1) {
            selectedList.push(item)
          }
        })
      } else {
        selectedList = arr;
      }
      selectedList.forEach(item => {
        this.tempFormInfo.tableData.push(item);
      })

      localStorage.setItem('tempFormInfo', JSON.stringify(this.tempFormInfo));
      this.$router.push("/warehouse/outhouse_ByTransport/create");

      // this.$router.push({
      //   path: '/wastereceipt/transfer/create',
      //   query: {
      //     AAname: "数组传递",
      //     mydata: selectedList
      //   }
      // });
    },
    async reset() {
      this.formInline = {
        name: "",
        codeContent: "",
        wpcName: "",
      };

      await this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
  .total-weight {
    position: absolute;
    bottom: 0;
    left: 0;
    line-height: 30px;
    vertical-align: middle;
  }

  .result {
    margin-top: 10px;
  }

  .page {
    margin-top: 20px;
    text-align: right;
    margin-bottom: 40px;
  }

  .show-result {
    margin-top: 10px;
    position: relative;
  }
</style>
